package com.industrial.operation.service.impl;

import cn.hutool.core.util.IdUtil;
import com.alibaba.excel.EasyExcel;
import com.industrial.common.core.utils.DateUtils;
import com.industrial.common.core.utils.StringUtils;
import com.industrial.common.security.utils.SecurityUtils;
import com.industrial.operation.domain.OperationCityCheckNew;
import com.industrial.operation.mapper.OperationCityCheckMapperNew;
import com.industrial.operation.service.IOperationCityCheckNewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class IOperationCityCheckNewServiceImpl implements IOperationCityCheckNewService {

    @Autowired
    private OperationCityCheckMapperNew operationCityCheckMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 查询十六市地市考核
     *
     * @param baseId 十六市地市考核主键
     * @return 十六市地市考核
     */
    @Override
    public OperationCityCheckNew selectOperationCityCheckByBaseId(String baseId)
    {
        return operationCityCheckMapper.selectOperationCityCheckByBaseId(baseId);
    }

    /**
     * 查询十六市地市考核列表
     *
     * @param operationCityCheckNew 十六市地市考核
     * @return 十六市地市考核
     */
    @Override
    public List<OperationCityCheckNew> selectOperationCityCheckList(OperationCityCheckNew operationCityCheckNew)
    {
        return operationCityCheckMapper.selectOperationCityCheckList(operationCityCheckNew);
    }

    /**
     * 新增十六市地市考核
     *
     * @param operationCityCheckNew 十六市地市考核
     * @return 结果
     */
    @Override
    public int insertOperationCityCheck(OperationCityCheckNew operationCityCheckNew)
    {
        return operationCityCheckMapper.insertOperationCityCheck(operationCityCheckNew);
    }

    /**
     * 修改十六市地市考核
     *
     * @param operationCityCheckNew 十六市地市考核
     * @return 结果
     */
    @Override
    public int updateOperationCityCheck(OperationCityCheckNew operationCityCheckNew)
    {
        return operationCityCheckMapper.updateOperationCityCheck(operationCityCheckNew);
    }

    /**
     * 批量删除十六市地市考核
     *
     * @param baseIds 需要删除的十六市地市考核主键
     * @return 结果
     */
    @Override
    public int deleteOperationCityCheckByBaseIds(String[] baseIds)
    {
        return operationCityCheckMapper.deleteOperationCityCheckByBaseIds(baseIds);
    }

    /**
     * 删除十六市地市考核信息
     *
     * @param baseId 十六市地市考核主键
     * @return 结果
     */
    @Override
    public int deleteOperationCityCheckByBaseId(String baseId)
    {
        return operationCityCheckMapper.deleteOperationCityCheckByBaseId(baseId);
    }

    /**
     * <p>
     * 导入数据模版文件
     * </p>
     *
     * @param file  文件
     * @param years 年份
     * @return
     */
    @Override
    public void importXlsxTemplate(MultipartFile file, String years) {

        try(InputStream fileIs = file.getInputStream()){
            List<OperationCityCheckNew> xlsxDataList = EasyExcel.read(fileIs).head(OperationCityCheckNew.class).headRowNumber(2).sheet().doReadSync();
            if(xlsxDataList == null || xlsxDataList.isEmpty()){
                throw new RuntimeException("没有加载到文件中的数据");
            }
            List<OperationCityCheckNew> list = new ArrayList<>(16);
            for(OperationCityCheckNew item : xlsxDataList){

                item.setYears(years);
                //设置ID
                item.setBaseId(IdUtil.simpleUUID());
                //设置创建人，创建时间、更新人、更新时间
                item.setBaseCreateBy(SecurityUtils.getUserId());
                item.setBaseCreateTime(DateUtils.getNowDate());
                item.setBaseUpdateBy(SecurityUtils.getUserId());
                item.setBaseUpdateTime(DateUtils.getNowDate());
                list.add(item);
                if(list.size() >= 100){
                    operationCityCheckMapper.batchInsertData(list);
                    list.clear();
                }
            }
            //批量入库
            if(!list.isEmpty()){
                operationCityCheckMapper.batchInsertData(list);
                list.clear();
            }
        }catch (Exception e){
            throw new RuntimeException(e.getMessage());
        }
    }

    /**
     * 查询统计的柱状图
     *
     * @param operationCityCheckNew
     * @return
     */
    @Override
    public Map<String, Object> queryTongJiEcharts(OperationCityCheckNew operationCityCheckNew) {
        List<OperationCityCheckNew> dbData = operationCityCheckMapper.queryTongJiEcharts(operationCityCheckNew);

        List<String> xAixs = new ArrayList<>();
        List<String> yAixs = new ArrayList<>();

        for(OperationCityCheckNew item : dbData){

            if(StringUtils.equals(item.getCityCode(),"全省")){
                continue;
            }

            String cityCode = item.getCityCode();
            xAixs.add(cityCode);

            String resultValue = item.getResultValue();
            yAixs.add(resultValue);
        }

        Map<String,Object> result = new HashMap<>(16);
        result.put("x",xAixs);
        result.put("y",yAixs);
        return result;
    }

    /**
     * 查询统计表格中的数据
     *
     * @param operationCityCheckNew
     * @return
     */
    @Override
    public List<OperationCityCheckNew> queryTongJiTable(OperationCityCheckNew operationCityCheckNew) {
        List<OperationCityCheckNew> dbData = operationCityCheckMapper.queryTongJiTable(operationCityCheckNew);
        return dbData;
    }
}
